<template>
  <div class="app-container">
    <el-form :model="queryParams" inline ref="queryForm" v-show="showSearch" label-width="96px" class="form-flex">
      <el-form-item label="所属小区" prop="communityId">
        <el-select v-model="queryParams.communityId" placeholder="请选择所属小区">
          <el-option v-for="dict in communityOptions" :key="dict.id" :label="dict.communityName" :value="dict.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属房屋" prop="roomId">
        <el-select v-model="queryParams.roomId" placeholder="请选择所属房屋">
          <el-option v-for="dict in roomList" :key="dict.id" :label="dict.roomName" :value="dict.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="customName">
        <el-input v-model="queryParams.customName"
          placeholder="请输入客户名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="费用类型" prop="chargeIds">
        <el-select v-model="queryParams.chargeIds" multiple="true" clearable placeholder="请选择费用类型">
          <el-option v-for="dict in chartTimeOptions" :key="dict.chargeId" :label="dict.chargeName" :value="dict.chargeId" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="截止日期" prop="beginTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.beginTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择截止日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="payRecord" v-hasPermi="['payfee:requireRecord:list']">催缴记录</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="RequirePay"   v-hasPermi="['payfee:requirePay:add']">发送短信</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="wxRequirePay"   v-hasPermi="['payfee:requirePay:add']">发送微信</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="PhoneRequirePay"  v-hasPermi="['payfee:requirePay:add']">发送APP</el-button>
      </el-col>
    </el-row>

    <el-table v-if="listData.length > 0" v-loading="loading" :data="listData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <template  v-for="row in listColumns">
        <el-table-column  :prop="row.key" :label="row.title"  show-overflow-tooltip>
        </el-table-column>
      </template>
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRecord(scope.row)"
          >催缴记录</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-empty v-else description="暂无数据"></el-empty>

    <note-form ref="noteForm"/>
    <wx-form ref="wxForm"/>
    <pay-record ref="payrecord"/>
    <phone-form ref="phoneForm"/>
    <record ref="record"/>
  </div>
</template>

<script>

  import {listMixin} from "@/utils/mixins/listMixin";
  import {requireBillRommCensus, roomArrearageList} from "@/api/payfee/requirePay";
  import { listCommunity } from "@/api/property/community";
  import {listNorm} from "@/api/payfee/norm";
  import {listRoom} from "@/api/property/room";
  import noteForm from "./noteForm"
  import wxForm from "./wxForm"
  import  payRecord from "./payRecord"
  import  phoneForm from "./phoneForm"
  import {listItem} from "@/api/payfee/item";
  import Record from "./Record";
  export default {
    name: "RequirePay",
    mixins: [listMixin],
    dicts: ['floor_state', 'housing_status'],
    components:{noteForm,wxForm,payRecord,phoneForm,Record},
    data() {
      return {
        //小区字典
        communityOptions:[],
        //欠费记录
        listData: [],
        roomList:[],
        chartTimeOptions:[],
        // 查询参数
        queryParams: {
          communityId: null,
          beginTime:null,
          roomId:null,
          customName:null,
          chargeIds:null,
        },
        listColumns: [],
        list: [],
      };
    },
    watch:{
      'queryParams.communityId'(v){
        listRoom({'communityId':this.queryParams.communityId}).then(res=>{
            this.roomList = res.rows;
        })
        listItem({'communityId':this.queryParams.communityId}).then(res=>{
          this.chartTimeOptions = res.rows;
        })
      }
    },
    created() {
      listCommunity({}).then(res=>{
        this.communityOptions = res.rows
      })
    },
    methods: {
      handleNodeClick(node) {
        this.currentNode = node;
        this.queryParams.nodeId = node.id
        this.getList()
      },
      /** 查询催缴管理列表 */
      getList() {
        if(!this.queryParams.communityId){
          this.msgWarning("请选择所属小区")
          return false
        }
        this.initListColumns()
      },
      initListColumns() {
        listNorm({deptId: this.queryParams.communityId}).then(res => {
          let listColumns = [
            {title: '房间名称', key: 'roomName'},
            {title: '客户名称', key: 'customName'},
            {title: '欠月数', key: 'difMonth'},
            {title: '欠本月', key: 'byAmountOwed'},
            {title: '欠往月', key: 'wyAmountOwed'},
          ]
          for (let i in res.rows) {
            let row = res.rows[i]
            let index = listColumns.findIndex(x => x.key == row.chargeId)
            if (index == -1) {
              listColumns.push({
                title: row.chargeName,
                key: row.chargeId,
              })
            }
          }
          listColumns.push({title: '合计', key: 'sumAmout'})
          this.listColumns = listColumns
          this.$nextTick(() => {
            this.loading = true;
            roomArrearageList(this.queryParams).then(res => {
              this.listData = res.rows
              this.loading = false;
            })
          })
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.list = selection;
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      RequirePay() {
        if (this.list.length == 0) {
          this.msgWarning("请选择催缴房间")
        } else {
          this.$refs.noteForm.show({requireRecordList: this.list, type: 'ADD'})
        }

      },
      handleRecord(row){
        this.$refs.record.show({rows:row})
      },
      wxRequirePay() {
        if (this.list.length == 0) {
          this.msgWarning("请选择催缴房间")
        } else {
          this.$refs.wxForm.show({requireRecordList: this.list, type: 'ADD'})
        }
      },
      payRecord() {
        this.$refs.payrecord.show();
      },
      PhoneRequirePay() {
        if (this.list.length == 0) {
          this.msgWarning("请选择催缴房间")
        } else {
          this.$refs.phoneForm.show({requireRecordList: this.list, type: 'ADD'})
        }
      }
    }
  };
</script>
